package com.tianxinweb.controller;


import com.tianxinweb.entity.ExchangeRate;
import com.tianxinweb.entity.Result;
import com.tianxinweb.enums.ResultEnum;
import com.tianxinweb.exception.MyException;
import com.tianxinweb.service.ExchangeRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cboy
 * @since 2018-08-07
 */
@RestController
@RequestMapping("/exchangeRate")
public class ExchangeRateController {

    @Autowired
    private ExchangeRateService service;

    /**
     * 获取黑金兑换率信息
     * @return
     */
    @RequestMapping("getExchangeRateInfo")
    public Result getExchangeRateInfo(){
        ExchangeRate exchangeRate = service.selectById(1);
        return  Result.success().add("rate",exchangeRate);
    }

    /**
     * 修改黑金兑换率
     * @param rate
     * @return
     */
    @PostMapping("setExchangeRate")
    public Result setExchangeRate(@RequestParam("rate") BigDecimal rate){
        if(rate.compareTo(new BigDecimal(0))==-1){
            throw new MyException(ResultEnum.PARAMETER_FORMAT_ERROR);
        }
        ExchangeRate exchangeRate = new ExchangeRate();
        exchangeRate.setId(1);
        exchangeRate.setRate(rate);
        service.updateById(exchangeRate);
        return  Result.success();
    }


}

